/*
 * PopupCanvassWidget.cpp
 *
 *  Created on: Jul 16, 2012
 *      Author: guillou
 */

#include <PopupCanvassWidget.hpp>
#include <PopupCanvassChoice.hpp>
#include <PopupCanvassResult.hpp>

using namespace std;
using namespace Popup;

PopupCanvassWidget::PopupCanvassWidget(QWidget *p_parent,
                                       const Popup::Canvass & p_canvass)
 : QWidget(p_parent), canvass(p_canvass), m_nbVotesCollected(0)
{
  setupUi(this);

  m_endOpinionRequest->setVisible(false);

  m_question->setText(p_canvass.question.c_str());
  timeEdit->setTime(QTime(0,0).addSecs(p_canvass.timeout * 60));

  ChoiceList::const_iterator _it;
  unsigned short _index = 0;

  m_nbVotesCollectedLabel->setText("0/" + QString::number(canvass.targets.size()));

  // Add choice widgets
  for (_index = 0, _it = p_canvass.choices.begin();
      _it != p_canvass.choices.end();
      _index++, _it++)
  {
    m_vbox->addWidget(new PopupChoiceWidget(this, _index, *_it));
  }

  // Add results widgets (make them not visible for now!)
  for (_index = 0, _it = p_canvass.choices.begin();
      _it != p_canvass.choices.end();
      _index++, _it++)
  {
    PopupCanvassResult *_resultWidget =
        new PopupCanvassResult(this, *_it, p_canvass.targets.size(),
                               p_canvass.isAnonymous);
    m_vbox->addWidget(_resultWidget);
    _resultWidget->setVisible(false);
    resultWidgets[_index] = _resultWidget;
  }

  QSpacerItem *_spacer = new QSpacerItem(40, 20,
                                         QSizePolicy::Expanding,
                                         QSizePolicy::Expanding);
  m_vbox->addItem(_spacer);

  connect(m_voteBtn, SIGNAL(clicked()), this, SLOT(slot_vote()));
  connect(&_timer, SIGNAL(timeout()), this, SLOT(slot_updateTimeLeft()));

  _timer.start(1000);
}

void PopupCanvassWidget::slot_updateTimeLeft() {
  QTime _newtime = timeEdit->time().addSecs(-1);
  timeEdit->setTime(_newtime);
  if (_newtime == QTime(0,0)) {
    timeEdit->setEnabled(false);
    _timer.stop();
    m_endOpinionRequest->setVisible(false);
    if (m_voteBtn->isVisible()) {
      showResults();
    }
  }
}

void PopupCanvassWidget::onChoiceToggled(PopupChoiceWidget *p_choice, bool status)
{
 if (status == true)
 {
   if (!canvass.allowMultiSelection) {
     std::set<PopupChoiceWidget*>::iterator _it;
     for (_it = selectedChoices.begin(); _it != selectedChoices.end(); _it++) {
       (*_it)->unselect();
     }
     selectedChoices.clear();
   }
   selectedChoices.insert(p_choice);
 }
 else
 {
   selectedChoices.erase(p_choice);
 }
}

void PopupCanvassWidget::slot_vote()
{
  Vote _vote;
  _vote.canvassID = canvass.canvassID;
  std::set<PopupChoiceWidget*>::iterator _it;
  for (_it = selectedChoices.begin(); _it != selectedChoices.end(); _it++) {
    _vote.choices.push_back((*_it)->index());
  }
  emit signal_sendVote(&_vote);

  showResults();
}

void PopupCanvassWidget::processVote(const Vote & p_vote)
{
  m_nbVotesCollected++;
  m_nbVotesCollectedLabel->setText(QString::number(m_nbVotesCollected) + "/" +
                                   QString::number(canvass.targets.size()));
  ChoiceIndexList::const_iterator _it;
  for (_it = p_vote.choices.begin(); _it != p_vote.choices.end(); _it++) {
    resultWidgets[*_it]->addVote(p_vote.voterID);
  }
}

void PopupCanvassWidget::showResults()
{
  m_voteBtn->setVisible(false);

  for (unsigned int _i = 0; _i < canvass.choices.size(); _i++) {
      delete m_vbox->takeAt(0)->widget();
  }

  std::map<unsigned short, PopupCanvassResult*>::const_iterator _itW;
  for (_itW = resultWidgets.begin(); _itW != resultWidgets.end(); _itW++) {
    _itW->second->setVisible(true);
  }
}
